const app = getApp()
const globalInclude = require('../static/global')
const Utils = require('../utils/compile/utils')

let $global = globalInclude.$global

module.exports = Behavior({
  data: {
  },
  methods: {
    schoolDepartmentMajorInit(key) {
      this.$set({
        [key]: {
          origin: [],
          departments: [],
          majors: []
        }
      })
    },
    schoolDepartmentMajorClearOptions(key) {
      this.$set({
        [key]: {
          origin: [],
          departments: [],
          majors: []
        }
      })
    },
    schoolDepartmentMajorStart(key, schoolUUID, departmentUUID, majorUUID) {
      let departmentIndex = -1
      let majorIndex = -1
      return new Promise((resolve, reject) => {
        this.schoolDepartmentMajorFetch(key, schoolUUID).then(({options}) => {
          if (!options) {
            options = []
          }
          departmentIndex = options.findIndex(option => {
            return option.value.uuid === departmentUUID
          })
          if (departmentIndex > -1) {
            this.schoolDepartmentMajorOnFormItemChange(key, 'department', departmentIndex)
              .then(({ majors }) => {
                if (!majors) {
                  majors = []
                }
                // console.log('majors', majors)
                majorIndex = majors.findIndex(major => {
                  return major.major_uuid === majorUUID
                })
                resolve({
                  majorIndex,
                  departmentIndex
                })
              })
          } else {
            resolve({
              majorIndex,
              departmentIndex
            })
          }
        })
      })
    },
    schoolDepartmentMajorFetch(key, uuid, d) {
      return new Promise((resolve, reject) => {
        $global.Api.getSchoolDepartmentMajor({
          id: uuid
        }).then(res => {
          let options = res.data.map((v, index) => {
            v._index = index
            return {
              label: v.name,
              value: v
            }
          })
          this.$set({
            [`${key}.origin`]: res.data,
            [`${key}.departments`]: options,
            [`${key}.majors`]: [],
            ...d
          })
          wx.nextTick(() => {
            resolve({
              options,
            })
          })
        })
      })
    },
    schoolDepartmentMajorOnFormItemChange(key, type, index, d) {
      return new Promise((resolve, reject) => {
        let originData = this.data[key].origin
        if (type === 'department') {
          this.$set({
            [`${key}.majors`]: originData[index]
              .schoolmajor.map((v) => {
                return {
                  label: v.name,
                  value: v
                }
              }),
            ...d
          })
          wx.nextTick(() => {
            resolve({
              majors: originData[index]
                .schoolmajor
            })
          })
        }
      })
    }
  }
})
